extends Node2D

@export var all_player_type_map_tres: AllPlayerTypeMap

func _on_tree_entered():
	Globals.player_type = AllPlayerTypeMap.PLAYER_TYPE.WARRIOR
	Globals.player_state = (
		all_player_type_map_tres.player_in_battle[Globals.player_type].create_instance()
	)
	$BattleMain.current_monster_group_id = "G0"
	
func _on_button_pressed():
	#await card_03_test()
	await card_26_test()
	
func card_03_test():
	var tween = create_tween()
	var card_id = "3"
	tween.tween_callback(draw_card.bind(card_id))
	tween.tween_callback(release_card.bind(1)).set_delay(1.0)
	tween.tween_callback(
		func():
			var monster:Monster = Globals.current_monster
			var state = monster.state_bar.get_state(StateBar.STATE.ARMOR_BREAK)
			assert(state != null)
			assert(state.state.id == 3)
			assert(state.state.value != null)
			var damage = monster.state.max_health - monster.state.health
			assert(TestGlobals.new_draw_card_card.damage == damage)
			assert(monster.state.armor_break == TestGlobals.new_draw_card_card.armor_break)
	).set_delay(1.1)
	tween.tween_callback(init).set_delay(1.0)
	await tween.finished

func card_26_test():
	var tween = create_tween()
	var card_id = "5"
	tween.tween_callback(draw_card.bind(card_id))
	tween.tween_callback(release_card.bind(1)).set_delay(1.0)
	card_id = "5"
	tween.tween_callback(draw_card.bind(card_id))
	tween.tween_callback(release_card.bind(1)).set_delay(1.0)
	card_id = "26"
	tween.tween_callback(draw_card.bind(card_id))
	
	#tween.tween_callback(init).set_delay(1.0)
	await tween.finished


func draw_card(card_id):
	for i in CardManager.init_hand_pool_tres.warrior_trophy_cards:
		if i.card_id == int(card_id):
			CardManager.draw_pool.add_card_front(i.duplicate())
			Events.card_table_drad_one_card_no_shuffle.emit()
			
func release_card(m_id):
	Globals.current_monster = TestGlobals.monster_handler.get_children()[m_id]
	var new_draw_card = TestGlobals.new_draw_card
	TestGlobals.new_draw_card_card = TestGlobals.new_draw_card.card
	var current_state = new_draw_card.card_state_machine.current_state
	current_state.transition_requested.emit(current_state, CardState.State.STATE_RELEASE)
	
func init():
	CardManager.restart()
	var new_battle_main = load("res://src/battle/battle_main.tscn").instantiate()
	new_battle_main.current_monster_group_id = "G0"
	var battle_main = get_node("BattleMain")
	if battle_main != null:
		remove_child(battle_main)
		battle_main.queue_free()
	add_child(new_battle_main)
